//
//  GlobalTools.m
//  FancyForum
//
//  Created by ChaiJun on 15-1-31.
//  Copyright (c) 2015年 Pinssible Labs. All rights reserved.
//

#import "TimeUtils.h"

@implementation TimeUtils

+ (NSString *)daysFromTodayByTime:(NSDate *)pastTime {
    
    NSDate *dateNow = [NSDate date];//Now
    
    NSTimeInterval time = [dateNow timeIntervalSinceDate:pastTime];
    
    int years = ((int)time)/(3600*24*30*365);
    
    int months = ((int)time)/(3600*24*30);
    
    int days=((int)time)/(3600*24);
    
    int hours=((int)time)%(3600*24)/3600;
    
    int minutes = ((int)time)%(3600*24)%3600/60;
    
//    int seconds = ((int)time)%(3600*24)%3600%60;
    
    NSString *dateContent;
    if (years >= 1) {
        dateContent = [NSString stringWithFormat:@"%i %@ ago", years, (years == 1)?@"year":@"years"];
    }
    else if (months >= 1) {
        dateContent = [NSString stringWithFormat:@"%i %@ ago",months, (months == 1)?@"month":@"months"];
    }
    else if (days >= 1) {
        dateContent = [NSString stringWithFormat:@"%i %@ ago",days, (days == 1)?@"day":@"days"];
    }
    else if (hours >= 1) {
        dateContent = [NSString stringWithFormat:@"%i %@ ago",hours, (hours == 1)?@"hour":@"hours"];
    }
    else if (minutes >= 1) {
        dateContent = [NSString stringWithFormat:@"%i %@ ago",minutes, (minutes == 1)?@"minute":@"minutes"];
    }
    else {
        dateContent = [NSString stringWithFormat:@"Seconds ago"];
    }
    
    return dateContent;
}

+ (NSString *)NSDateToNSStringReformer:(NSDate *)date {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *strDate = [dateFormatter stringFromDate:date];
    
    return strDate;
}

+ (NSString *)ageByTime:(NSDate *)createdTime
{
    NSDate *dateNow = [NSDate date];//Now
    
    NSTimeInterval time = [dateNow timeIntervalSinceDate:createdTime];
    
    int years = ((int)time)/(3600*24*30*365);
    
    int months = (((int)time)/(3600*24*30)%12);
    
    int days=(((int)time)/(3600*24)%30);
    
    NSString *ageContent;
    if (years >= 1) {
        ageContent = [NSString stringWithFormat:@"%i %@", years, (years > 1)?@"years":@"year"];
    }
    else if (months > 0) {
        ageContent = [NSString stringWithFormat:@"%i %@", months, (months > 1)?@"months":@"month"];
    }
    else if (days > 0) {
        ageContent = [NSString stringWithFormat:@"%i %@", days, (days > 1)?@"days":@"day"];
    }
    else {
        ageContent = [NSString stringWithFormat:@"Less than 1 day"];
    }
    
    return ageContent;
}

+ (NSString *)timeLeft:(NSUInteger)miliSeconds
{
    int seconds = (int)miliSeconds / 1000;
    
    int hours = seconds / 3600;
    
    int minutes = seconds % 60;
    
    NSString *timeLeft;
    if (hours >= 1) {
        timeLeft = [NSString stringWithFormat:@"%i %@", hours, (hours > 1)?@"hours":@"hour"];
    }
    else if (minutes >= 1) {
        timeLeft = [NSString stringWithFormat:@"%i %@", minutes, (minutes > 1)?@"minutes":@"minute"];
    }
    else {
        timeLeft = @"less than 1 minute";
    }
    
    return [NSString stringWithFormat:@"Timeleft: %@", timeLeft];
}

+ (NSInteger)daysFromToday:(NSDate *)pastTime
{
    NSDate *dateNow = [NSDate date];//Now
    
    NSTimeInterval time = [dateNow timeIntervalSinceDate:pastTime];
    
    return ((NSInteger)time)/(3600*24);
}

+ (NSString *)stringFromDate:(NSDate *)date {
    NSDate *currentDate = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss zzz"];
    NSString *currentDateString = [dateFormatter stringFromDate:currentDate];
    
    return currentDateString;
}

+ (NSDate *)dateFromString:(NSString *)string {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss zzz"];
    NSDate *date = [formatter dateFromString:string];
    
    return date;
}

+ (NSNumber *)secondsFromGMT {
    NSTimeZone *localTimeZone = [NSTimeZone localTimeZone];
    NSNumber *secondsFromGMT = [NSNumber numberWithInteger:localTimeZone.secondsFromGMT];
    
    return secondsFromGMT;
}

@end
